import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";

const useAllLogFiles = () => {
    const { remoteData } = useModel("global");
    const { selectedGateway } = useModel("gateway.info");

    const state = useReactive({
        loading: false,
        logFiles: [],
    });

    const loadAllLogFiles = async () => {
        if (!selectedGateway.url) return;
        state.loading = true;
        let res = await remoteData({
            object: "multi-person-gateway",
            p2pNodeUuid: selectedGateway.uuid,
            serviceUrl: selectedGateway.url,
            serviceName: "/logs/getFiles",
            params: {},
        });
        state.logFiles = res || [];
        state.loading = false;
    };

    useEffect(() => {
        selectedGateway?.url && loadAllLogFiles();
    }, [selectedGateway]);

    return { ...state, loadAllLogFiles };
};

export default useAllLogFiles;
